﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Communication.Base;
using UtilZ.Dotnet.Ex.ZLog;

namespace UtilZ.Dotnet.Ex.Communication.Net.Base
{
    /// <summary>
    /// 网络连接信息
    /// </summary>
    public class NetLinkInfo : LinkInfoAbs
    {
        /// <summary>
        /// 获取远程终结点
        /// </summary>
        public EndPoint RemoteEndPoint { get; set; }

        /// <summary>
        /// 获取本地终结点
        /// </summary>
        public EndPoint LocalEndPoint { get; private set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="remoteEndPoint">来源地址，即发送数据地址</param>
        /// <param name="localEndPoint">获取本地终结点</param>
        public NetLinkInfo(EndPoint remoteEndPoint, EndPoint localEndPoint)
            : base()
        {
            this.RemoteEndPoint = remoteEndPoint;
            this.LocalEndPoint = localEndPoint;
        }


        /// <summary>
        /// 重写ToString
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            try
            {
                string local = NetHelper.GetIPPortString(LocalEndPoint);
                string remote = NetHelper.GetIPPortString(RemoteEndPoint);
                return $"Local:{local},Remote:{remote}";
            }
            catch (Exception ex)
            {
                ZLoger.Error(ex);
                return base.ToString();
            }
        }
    }
}
